package com.wanbangee.marksystem.service.imp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wanbangee.marksystem.dao.MAnswerSheetTemplateDao;
import com.wanbangee.marksystem.dao.MObjectiveAnswerDao;
import com.wanbangee.marksystem.dao.MSubjectDao;
import com.wanbangee.marksystem.dao.MSubjectiveSetDao;
import com.wanbangee.marksystem.entities.MObjectiveAnswer;
import com.wanbangee.marksystem.entities.MSubjectiveSet;
import com.wanbangee.marksystem.service.SettingTopicNoMangerService;
@Service
public class SettingTopicNoMangerServiceImp implements SettingTopicNoMangerService {
	@Autowired
	private MAnswerSheetTemplateDao  mAnswerSheetTemplateDao;
	@Autowired
	private MObjectiveAnswerDao mObjectiveAnswerDao;
	@Autowired
	private MSubjectDao mSubjectDao;
	@Autowired
	private MSubjectiveSetDao mSubjectiveSetDao;
	//模板总分
	private Double scoreTotal ;
	
	
	@Override
	public void insertIntoMObjectiveAnswerAndMSubjectiveSetBySelectMAnswerSheetTemplate() {
		// TODO Auto-generated method stub
		this.mAnswerSheetTemplateDao.insertIntoMObjectiveAnswerBySelectMAnswerSheetTemplate();
		this.mAnswerSheetTemplateDao.insertIntoMSubjectiveSetBySelectMAnswerSheetTemplate();
	}

	@Override
	public Map<String,Object> selectTopicScoreTempTotal(Integer examId, Integer examSubId) {
		// TODO Auto-generated method stub
		scoreTotal = this.mAnswerSheetTemplateDao.selectTopicScoreActalTotal(examId, examSubId);
		Map<String,Object> map = new HashMap<>();
		map.put("scoreTotal", scoreTotal);
		return map;
	}

	@Override
	public Map<String,Object> selectMObjectiveAnswer(Integer examId, Integer examSubId) {
		// TODO Auto-generated method stub
		List<MObjectiveAnswer> mObjectiveAnswers = this.mObjectiveAnswerDao.selectMObjectiveAnswer(examId, examSubId);
		Map<String,Object> map = new HashMap<>();
		map.put("mObjectiveAnswers", mObjectiveAnswers);
		return map;
	}

	@Override
	public Map<String,Object> updateMObjectiveAnswer(String rightAnswer, Double topicScore,Integer topicType, Integer isGainScore, Double gainScore,
		String topicNo, Integer examId, Integer examSubId) {
		// TODO Auto-generated method stub
		boolean falg = this.mObjectiveAnswerDao.updateMObjectiveAnswer(rightAnswer, topicScore, isGainScore,topicType, gainScore, topicNo, examId, examSubId);
		Map<String,Object> map = new HashMap<>();
		map.put("falg", falg);
		return map;
	}


	@Override
	public Map<String,Object> selectMSubjectiveSet(Integer examId, Integer examSubId) {
		// TODO Auto-generated method stub
		List<MSubjectiveSet> mSubjectiveSets= this.mSubjectiveSetDao.selectMSubjectiveSet(examId, examSubId);
		Map<String,Object> map = new HashMap<>();
		map.put("mSubjectiveSets", mSubjectiveSets);
		return map;
	}

	@Override
	public Map<String,Object> updateMSubjectiveSet(Double topicScore, Integer stepSize, String topicNo, Integer examId,
			Integer examSubId) {
		// TODO Auto-generated method stub
		boolean falg = this.mSubjectiveSetDao.updateMSubjectiveSet(topicScore, stepSize, topicNo, examId, examSubId);
		Map<String,Object> map = new HashMap<>();
		map.put("falg", falg);
		return map;
	}



	@Override
	public Map<String, Object> saveSetting(Integer examId, Integer examSubId) {
		// TODO Auto-generated method stub
		Map<String,Object> map = new HashMap<>();
		Double scoreActal = this.mAnswerSheetTemplateDao.selectTopicScoreActalTotal(examId, examSubId);
		if(this.scoreTotal!=scoreActal) {
			map.put("code", 10010);
			map.put("message", "总分不一致，保存失败");
			return map;
		}
		//改变科目状态
		this.mSubjectDao.updateMSubject(examId, examSubId);
		map.put("code", 1001);
		map.put("message","操作成功");
		return map;
	}

}
